
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>HTTP服务器 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="WebSocket服务器.html" />
    
    
    <link rel="prev" href="爬虫实战.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >HTTP服务器</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x6570;&#x636E;&#x5E93;"><b></b>&#x6570;&#x636E;&#x5E93;</a></li><ul><li><span class="title-icon "></span><a href="#postgresql"><b></b>PostgreSQL</a></li><li><span class="title-icon "></span><a href="#&#x5B89;&#x88C5;"><b></b>&#x5B89;&#x88C5;</a></li></ul><li><span class="title-icon "></span><a href="#dart-&#x670D;&#x52A1;&#x7AEF;"><b></b>Dart &#x670D;&#x52A1;&#x7AEF;</a></li><ul><li><span class="title-icon "></span><a href="#&#x5B89;&#x88C5;_1"><b></b>&#x5B89;&#x88C5;</a></li><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;&#x9879;&#x76EE;"><b></b>&#x521B;&#x5EFA;&#x9879;&#x76EE;</a></li><li><span class="title-icon "></span><a href="#&#x6700;&#x7B80;&#x793A;&#x4F8B;&#x2014;&#x2014;hello-world"><b></b>&#x6700;&#x7B80;&#x793A;&#x4F8B;&#x2014;&#x2014;hello world</a></li><li><span class="title-icon "></span><a href="#&#x8DEF;&#x7531;"><b></b>&#x8DEF;&#x7531;</a></li><li><span class="title-icon "></span><a href="#postgresql-orm"><b></b>PostgreSQL ORM</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="&#x6570;&#x636E;&#x5E93;"><a name="&#x6570;&#x636E;&#x5E93;" class="anchor-navigation-ex-anchor" href="#&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6570;&#x636E;&#x5E93;</h2>
<h3 id="postgresql"><a name="postgresql" class="anchor-navigation-ex-anchor" href="#postgresql"><i class="fa fa-link" aria-hidden="true"></i></a>PostgreSQL</h3>
<p>&#x8BFB;&#x97F3;&#xFF1A;&quot;post-gress-Q-L&quot;&#x3002;&#x88AB;&#x7B80;&#x7565;&#x5FF5;&#x4E3A; &quot;postgres&quot;&#xFF0C;&#x5B98;&#x65B9;&#x8BFB;&#x97F3;&#x6587;&#x4EF6; <a href="https://www.postgresql.org/files/postgresql.mp3" target="_blank">https://www.postgresql.org/files/postgresql.mp3</a></p>
<p><strong>&#x4EC0;&#x4E48;&#x662F;PostgreSQL&#xFF1F;</strong></p>
<p>PostgreSQL&#x662F;&#x4E00;&#x4E2A;&#x529F;&#x80FD;&#x5F3A;&#x5927;&#x7684;&#x5F00;&#x6E90;&#x5BF9;&#x8C61;&#x5173;&#x7CFB;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;(ORDBMS)&#x3002; &#x7528;&#x4E8E;&#x5B89;&#x5168;&#x5730;&#x5B58;&#x50A8;&#x6570;&#x636E;; &#x652F;&#x6301;&#x6700;&#x4F73;&#x505A;&#x6CD5;&#xFF0C;&#x5E76;&#x5141;&#x8BB8;&#x5728;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x65F6;&#x68C0;&#x7D22;&#x5B83;&#x4EEC;&#x3002;</p>
<p>&#x5B83;&#x7531;PostgreSQL&#x5168;&#x7403;&#x5F00;&#x53D1;&#x96C6;&#x56E2;(&#x5168;&#x7403;&#x5FD7;&#x613F;&#x8005;&#x56E2;&#x961F;)&#x5F00;&#x53D1;&#x3002; &#x5B83;&#x4E0D;&#x53D7;&#x4EFB;&#x4F55;&#x516C;&#x53F8;&#x6216;&#x5176;&#x4ED6;&#x79C1;&#x4EBA;&#x5B9E;&#x4F53;&#x63A7;&#x5236;&#x3002; &#x5B83;&#x662F;&#x5F00;&#x6E90;&#x7684;&#xFF0C;&#x5176;&#x6E90;&#x4EE3;&#x7801;&#x662F;&#x514D;&#x8D39;&#x63D0;&#x4F9B;&#x7684;&#x3002;&#x5B83;&#x4E5F;&#x662F;&#x8DE8;&#x5E73;&#x53F0;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x8BB8;&#x591A;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E0A;&#x8FD0;&#x884C;&#xFF0C;&#x5982;Linux&#xFF0C;FreeBSD&#xFF0C;OS X&#xFF0C;Solaris&#x548C;Microsoft Windows&#x7B49;&#x3002;</p>
<h3 id="&#x5B89;&#x88C5;"><a name="&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B89;&#x88C5;</h3>
<p><a href="https://www.postgresql.org/download/windows/" target="_blank">&#x4E0B;&#x8F7D;&#x94FE;&#x63A5;</a></p>
<h2 id="dart-&#x670D;&#x52A1;&#x7AEF;"><a name="dart-&#x670D;&#x52A1;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#dart-&#x670D;&#x52A1;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>Dart &#x670D;&#x52A1;&#x7AEF;</h2>
<p>&#x4F7F;&#x7528;Dart&#x8BED;&#x8A00;&#x7684;&#x539F;&#x751F;API&#x5F00;&#x53D1;HTTP&#x670D;&#x52A1;&#x5668;&#x4ECD;&#x663E;&#x5F97;&#x8FC7;&#x4E8E;&#x7E41;&#x7410;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A;HTTP&#x670D;&#x52A1;&#x5668;&#x6846;&#x67B6;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x53EA;&#x9700;&#x8981;&#x5173;&#x6CE8;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x7684;&#x5904;&#x7406;&#x3002;&#x5982;&#x679C;&#x5927;&#x5BB6;&#x4F7F;&#x7528;&#x8FC7;&#x4EFB;&#x4F55;&#x4E00;&#x6B3E;&#x6210;&#x719F;&#x7684;HTTP&#x670D;&#x52A1;&#x5668;&#x6846;&#x67B6;&#xFF0C;&#x90A3;&#x4E48;&#x5BF9;&#x4E8E;&#x65B0;&#x6846;&#x67B6;&#x4E0A;&#x624B;&#x5C31;&#x4F1A;&#x6613;&#x5982;&#x53CD;&#x638C;&#xFF0C;&#x56E0;&#x4E3A;&#x7EDD;&#x5927;&#x591A;&#x6570;&#x670D;&#x52A1;&#x5668;&#x6846;&#x67B6;&#x7684;&#x6982;&#x5FF5;&#x90FD;&#x662F;&#x76F8;&#x540C;&#x7684;&#xFF0C;&#x4E3B;&#x8981;&#x5C31;&#x662F;<code>ORM</code>&#x3001;&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x3001;&#x6A21;&#x677F;&#x6E32;&#x67D3;&#x3001;&#x4E2D;&#x95F4;&#x4EF6;&#x7B49;&#x7B49;&#x8FD9;&#x4E9B;&#x4E1C;&#x897F;&#x3002;</p>
<p>&#x6839;&#x636E;&#x6211;&#x6240;&#x77E5;&#x7684;&#xFF0C;&#x76EE;&#x524D;&#x53EF;&#x7528;&#x7684;&#x4ECD;&#x5728;&#x7EF4;&#x62A4;&#x7684;Dart&#x7684;HTTP&#x670D;&#x52A1;&#x5668;&#x6846;&#x67B6;&#x4E3B;&#x8981;&#x6709;&#x56DB;&#x4E2A;&#xFF0C;&#x4F9D;&#x6B21;&#x6309;&#x7167;star&#x6700;&#x591A;&#x7684;&#x4ECE;&#x4E0A;&#x5230;&#x4E0B;&#x6765;&#x6392;&#x5E8F;&#xFF1A;</p>
<ul>
<li><a href="https://github.com/stablekernel/aqueduct" target="_blank"><strong>aqueduct</strong></a></li>
<li><strong><a href="https://github.com/angel-dart/angel" target="_blank">angel</a></strong></li>
<li><strong><a href="https://github.com/Jaguar-dart/jaguar" target="_blank">jaguar</a></strong></li>
<li><strong><a href="https://github.com/dart-lang/shelf" target="_blank">shelf</a></strong></li>
</ul>
<p>&#x5176;&#x4E2D;&#x6392;&#x7B2C;&#x4E00;&#x7684; <strong>aqueduct</strong> &#x662F;&#x529F;&#x80FD;&#x3001;&#x6587;&#x6863;&#x3001;&#x793A;&#x4F8B;&#x6700;&#x5B8C;&#x5584;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5C31;&#x4EE5;&#x6B64;&#x6846;&#x67B6;&#x505A;&#x6F14;&#x793A;</p>
<h3 id="&#x5B89;&#x88C5;_1"><a name="&#x5B89;&#x88C5;_1" class="anchor-navigation-ex-anchor" href="#&#x5B89;&#x88C5;_1"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B89;&#x88C5;</h3>
<pre><code class="lang-shell">pub global activate aqueduct
</code></pre>
<h3 id="&#x521B;&#x5EFA;&#x9879;&#x76EE;"><a name="&#x521B;&#x5EFA;&#x9879;&#x76EE;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x9879;&#x76EE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521B;&#x5EFA;&#x9879;&#x76EE;</h3>
<p>&#x6267;&#x884C;&#x547D;&#x4EE4;&#xFF0C;&#x751F;&#x6210;&#x9879;&#x76EE;<code>api_server</code></p>
<pre><code class="lang-shell">pub global run aqueduct create api_server
</code></pre>
<h3 id="&#x6700;&#x7B80;&#x793A;&#x4F8B;&#x2014;&#x2014;hello-world"><a name="&#x6700;&#x7B80;&#x793A;&#x4F8B;&#x2014;&#x2014;hello-world" class="anchor-navigation-ex-anchor" href="#&#x6700;&#x7B80;&#x793A;&#x4F8B;&#x2014;&#x2014;hello-world"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6700;&#x7B80;&#x793A;&#x4F8B;&#x2014;&#x2014;hello world</h3>
<p>&#x5176;&#x4E2D;<code>bin/main.dart</code>&#x4E0B;&#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x4FEE;&#x6539;&#xFF0C;&#x4E3B;&#x8981;&#x4FEE;&#x6539;<code>lib/channel.dart</code>&#xFF0C;&#x5220;&#x9664;&#x591A;&#x4F59;&#x6CE8;&#x91CA;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:api_server/controller.dart&apos;</span>;

<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;api_server.dart&apos;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ApiServerChannel</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ApplicationChannel</span> </span>{
  <span class="hljs-meta">@override</span>
  <span class="hljs-function">Future <span class="hljs-title">prepare</span><span class="hljs-params">()</span> async </span>{
    logger.onRecord.listen((rec) =&gt; print(<span class="hljs-string">&quot;$rec ${rec.error ?? &quot;</span><span class="hljs-string">&quot;} ${rec.stackTrace ?? &quot;</span><span class="hljs-string">&quot;}&quot;</span>));
  }

  <span class="hljs-meta">@override</span>
  Controller get entryPoint {
    <span class="hljs-keyword">final</span> router = Router();
    router
      .route(<span class="hljs-string">&quot;/&quot;</span>)
      .linkFunction((request) async {
        <span class="hljs-keyword">return</span> Response.ok(<span class="hljs-string">&apos;hello world!&apos;</span>);
      });
    <span class="hljs-keyword">return</span> router;
  }
}
</code></pre>
<p>&#x7B80;&#x5355;&#x8BF4;&#x4E00;&#x4E0B;&#xFF0C;&#x8FD9;&#x91CC;&#x6709;&#x4E24;&#x4E2A;&#x5B9E;&#x73B0;&#xFF0C;&#x5176;&#x4E2D;<code>prepare()</code>&#x65B9;&#x6CD5;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x9884;&#x5904;&#x7406;&#xFF0C;&#x4F8B;&#x5982;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x7B49;&#xFF0C;&#x6211;&#x4EEC;&#x6682;&#x65F6;&#x7528;&#x4E0D;&#x5230;&#xFF0C;&#x4E0D;&#x9700;&#x7406;&#x4F1A;&#x3002;<code>entryPoint</code>&#x65B9;&#x6CD5;&#x662F;&#x6211;&#x4EEC;&#x771F;&#x6B63;&#x9700;&#x8981;&#x5173;&#x6CE8;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x7684;&#x6267;&#x884C;&#x5728;<code>prepare()</code>&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#xFF0C;&#x5F53;&#x6709;&#x8BF7;&#x6C42;&#x5230;&#x6765;&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x56DE;&#x8C03;&#x3002;&#x6211;&#x4EEC;&#x5728;&#x8BE5;&#x65B9;&#x6CD5;&#x4E2D;&#x6CE8;&#x518C;&#x8DEF;&#x7531;&#xFF0C;&#x8FD9;&#x91CC;&#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x6839;&#x8DEF;&#x5F84;&#xFF0C;&#x5E76;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x8BF7;&#x6C42;&#x7684;&#x533F;&#x540D;&#x56DE;&#x8C03;&#x65B9;&#x6CD5;&#x3002;&#x5F53;&#x6211;&#x4EEC;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x8BBF;&#x95EE;<code>http://localhost:8888</code>&#x65F6;&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#xFF0C;&#x5373;&#x5411;&#x6D4F;&#x89C8;&#x5668;&#x6253;&#x5370;&#x4E00;&#x53E5;<code>hello world!</code></p>
<p><code>cd</code>&#x5230;&#x9879;&#x76EE;&#x6839;&#x8DEF;&#x5F84;&#x4E0B;&#xFF0C;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x542F;&#x52A8;&#x670D;&#x52A1;</p>
<pre><code class="lang-shell">dart bin/main.dart
</code></pre>
<p>&#x6216;&#x8005;</p>
<pre><code class="lang-shell">pub global run aqueduct serve
</code></pre>
<p>&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x8BBF;&#x95EE;<code>http://localhost:8888</code>&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x8F93;&#x51FA;<code>hello world!</code></p>
<h3 id="&#x8DEF;&#x7531;"><a name="&#x8DEF;&#x7531;" class="anchor-navigation-ex-anchor" href="#&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8DEF;&#x7531;</h3>
<p>Router&#x9664;&#x4E86;&#x53EF;&#x4EE5;&#x6CE8;&#x518C;&#x56DE;&#x8C03;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x5173;&#x8054;&#x4E00;&#x4E2A;Controller&#x7528;&#x4E8E;&#x5904;&#x7406;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#x3002;&#x81EA;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;Controller&#x3002;&#x5B83;&#x9700;&#x8981;&#x7EE7;&#x627F;&#x81EA;&#x6846;&#x67B6;&#x7684;Controller&#x7C7B;&#xFF0C;&#x5E76;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;handle&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:async&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:aqueduct/aqueduct.dart&apos;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ItemsController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span> </span>{

  <span class="hljs-meta">@override</span>
  Future&lt;RequestOrResponse&gt; handle(Request request) <span class="hljs-keyword">async</span> {
    <span class="hljs-keyword">final</span> content = <span class="hljs-keyword">await</span> File(<span class="hljs-string">&apos;asset/data.json&apos;</span>).readAsString();
    <span class="hljs-keyword">return</span> Response.ok(json.decode(content));
  }
}
</code></pre>
<p>&#x6CE8;&#x518C;&#x8DEF;&#x7531;</p>
<pre><code class="lang-dart">  <span class="hljs-meta">@override</span>
  Controller <span class="hljs-keyword">get</span> entryPoint {
    <span class="hljs-keyword">final</span> router = Router();
    router
      .route(<span class="hljs-string">&quot;/&quot;</span>)
      .linkFunction((request) <span class="hljs-keyword">async</span> {
        <span class="hljs-keyword">return</span> Response.ok(<span class="hljs-string">&apos;hello world!&apos;</span>);
      });

    <span class="hljs-comment">// &#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x65B0;&#x7684;url&#xFF0C;&#x5E76;&#x5173;&#x8054;&#x5230;&#x6211;&#x4EEC;&#x81EA;&#x5B9A;&#x4E49;&#x7684;Controller&#x4E0A;</span>
    router
    .route(<span class="hljs-string">&apos;/api/all&apos;</span>)
    .link(() =&gt; ItemsController());

    <span class="hljs-keyword">return</span> router;
  }
</code></pre>
<p>&#x901A;&#x5E38;&#x7684;&#xFF0C;&#x6D89;&#x53CA;&#x5230;&#x66F4;&#x590D;&#x6742;&#x8BF7;&#x6C42;&#xFF0C;&#x5982;<code>post</code>&#x7B49;&#xFF0C;&#x5E94;&#x7EE7;&#x627F;&#x81EA;<code>ResourceController</code>&#xFF0C;&#x901A;&#x8FC7;&#x6CE8;&#x89E3;&#x5B9A;&#x4E49;&#x8BF7;&#x6C42;&#x7C7B;&#x578B;</p>
<pre><code class="lang-dart"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ItemDataController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ResourceController</span></span>{
  ItemDataController(<span class="hljs-keyword">this</span>.context);
  <span class="hljs-keyword">final</span> ManagedContext context;

  <span class="hljs-meta">@Operation</span>.<span class="hljs-keyword">get</span>()
  Future&lt;Response&gt; getItems(<span class="hljs-meta">@Bind</span>.query(<span class="hljs-string">&apos;offset&apos;</span>) <span class="hljs-built_in">int</span> offset, <span class="hljs-meta">@Bind</span>.query(<span class="hljs-string">&apos;limit&apos;</span>) <span class="hljs-built_in">int</span> limit) <span class="hljs-keyword">async</span> {
    <span class="hljs-comment">// &#x67E5;&#x8BE2;&#x6570;&#x636E;&#x5E93;</span>
    <span class="hljs-keyword">final</span> query = Query&lt;Paragraph&gt;(context)
    ..offset = offset
    ..fetchLimit = limit;
    <span class="hljs-comment">// &#x8FD4;&#x56DE;json&#x7ED3;&#x679C;</span>
    <span class="hljs-keyword">return</span> Response.ok(<span class="hljs-keyword">await</span> query.fetch());
  }
</code></pre>
<h3 id="postgresql-orm"><a name="postgresql-orm" class="anchor-navigation-ex-anchor" href="#postgresql-orm"><i class="fa fa-link" aria-hidden="true"></i></a>PostgreSQL ORM</h3>
<p>&#x5B9A;&#x4E49;&#x4E0E;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x8868;&#x6620;&#x5C04;&#x7684;&#x5B9E;&#x4F53;&#x7C7B;</p>
<pre><code class="lang-dart"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Paragraph</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ManagedObject</span>&lt;<span class="hljs-title">_Paragraph</span>&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title">_Paragraph</span> </span>{}

<span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8868;&#x540D;&#x79F0;</span>
<span class="hljs-meta">@Table</span>(name: <span class="hljs-string">&quot;paragraph&quot;</span>)
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_Paragraph</span> </span>{
  <span class="hljs-comment">// &#x8868;&#x6620;&#x5C04;&#x7684;&#x5B9E;&#x4F53;&#x7C7B;</span>
  <span class="hljs-comment">// &#x5C5E;&#x6027;&#x5BF9;&#x5E94;&#x8868;&#x4E2D;&#x5B57;&#x6BB5;</span>
  <span class="hljs-meta">@primaryKey</span>
  <span class="hljs-built_in">int</span> id;
  <span class="hljs-built_in">String</span> nick_name ;
  <span class="hljs-built_in">String</span> avatar ;
  <span class="hljs-built_in">String</span> content ;
  <span class="hljs-built_in">String</span> img_url ;
  <span class="hljs-built_in">String</span> date ;
  <span class="hljs-built_in">int</span> like_no ;
  <span class="hljs-built_in">int</span> tap_no ;
}
</code></pre>
<p>&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</p>
<pre><code class="lang-dart">  <span class="hljs-meta">@override</span>
  Future prepare() <span class="hljs-keyword">async</span> {
    <span class="hljs-comment">// &#x8BFB;&#x53D6;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span>
    <span class="hljs-keyword">final</span> config = AppConfiguration.fromFile(File(options.configurationFilePath));
    <span class="hljs-keyword">final</span> db = config.database;

    <span class="hljs-keyword">final</span> persistentStore = PostgreSQLPersistentStore.fromConnectionInfo(
        db.username, db.password, db.host, db.port, db.databaseName);

    <span class="hljs-comment">// &#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</span>
    context = ManagedContext(
        ManagedDataModel.fromCurrentMirrorSystem(), persistentStore);
  }
</code></pre>
<p>&#x66F4;&#x591A;&#x7EC6;&#x81F4;&#x529F;&#x80FD;&#x53EF;&#x53C2;&#x8003; <a href="https://aqueduct.io/docs/tour/" target="_blank">aqueduct&#x5B98;&#x65B9;&#x6587;&#x6863;</a> 
&#x540E;&#x7EED;&#x6D89;&#x53CA;&#x5230;Flutter&#x8BFE;&#x7A0B;&#x540E;&#xFF0C;&#x4F1A;&#x518D;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x5F00;&#x53D1;&#x6CE8;&#x518C;&#x3001;&#x767B;&#x5F55;&#x4EE5;&#x53CA;&#x751F;&#x6210;<code>token</code>&#x7B49;&#x529F;&#x80FD;&#xFF0C;&#x4EE5;&#x4FBF;&#x4E8E;&#x914D;&#x5408;Flutter &#x6F14;&#x793A;<code>token</code>&#x7684;&#x50A8;&#x5B58;&#x548C;&#x4F7F;&#x7528;&#x3002;</p>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:35:01
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="爬虫实战.html" class="navigation navigation-prev " aria-label="Previous page: 爬虫实战">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="WebSocket服务器.html" class="navigation navigation-next " aria-label="Next page: WebSocket服务器">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"HTTP服务器","level":"1.5.3","depth":2,"next":{"title":"WebSocket服务器","level":"1.5.4","depth":2,"path":"WebSocket服务器.md","ref":"WebSocket服务器.md","articles":[]},"previous":{"title":"爬虫实战","level":"1.5.2","depth":2,"path":"爬虫实战.md","ref":"爬虫实战.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"HTTP服务器.md","mtime":"2021-03-18T05:35:01.521Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

